package com.idea.zd.sys.train.thread;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class LockTest implements Runnable{

    private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();
    int i;
    @Override
    public void run() {
        try{
            lock.lock();
            for (;i<100;i++){
                System.out.println(Thread.currentThread().getName()+":"+i);
                Thread.sleep(1000);


                condition.signal();//唤醒
                condition.await();//等侍
            }

        }catch (Exception ex){
            ex.printStackTrace();
        }finally{
            lock.unlock();
        }
    }

    public static void main(String [] args){
        LockTest lockTest = new LockTest();
        new Thread(lockTest,"第一个线程").start();
        new Thread(lockTest,"第二个线程").start();
    }
}
